<?php
ini_set("display_errors", "On"); 
error_reporting(E_ALL);
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../conf/conf.php');
require('../includes/init.php');
require('./classes/module/module_product_review.php');
require('./configs/site_config.php');

class ProductReview {
	private $web_type;
	private $menuid;
	private $mod;
	
	public function __construct( $web_type, $menuid ){
		global $menu_type;
		$this->web_type = (int)$web_type;
		$this->menuid = $menuid;
		
		if( !$this->web_type ) $this->web_type = $menu_type[$this->menuid];
		
		$this->mod = new ProductReviewModule();
	}
	
	//note 评论列表
	public function index(){
		$check_state = isset( $_GET['check_state'] ) ? $_GET['check_state'] : 0;
		$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
		
		//搜索
		$s_pid = isset( $_GET['s_pid'] ) ? $_GET['s_pid'] : '';
		$s_pname = isset( $_GET['s_pname'] ) ? $_GET['s_pname'] : '';
		
		$s_where = '';
		$pid_str = '';
		if( $s_pname != '' ){
			$pid_str = $this->mod->get_pid_by_name( $s_pname );
		}
		
		if( $pid_str ) $s_where = ' AND pid IN('.$pid_str.') ';
		if( $s_pid ) $s_where = ' AND pid = '.$s_pid;
		
		
		$num = 0;
		$limit = 20;
		$offset = ( $page - 1 ) * $limit;
		
		$data = $this->mod->get_list( $this->web_type, $check_state, $offset, $limit, $num, $s_where );//print_r($data);
		
		$pages = get_page_url( $page, ceil($num / $limit) );
		
		$GLOBALS['smarty']->assign( 'pages', ( $num > $limit ) ? $pages : '' );
		$GLOBALS['smarty']->assign( 'check_state', $check_state );
		$GLOBALS['smarty']->assign( 'web_type', $this->web_type );
		$GLOBALS['smarty']->assign( 's_pid', $s_pid );
		$GLOBALS['smarty']->assign( 's_pname', $s_pname );
		$GLOBALS['smarty']->assign( 'data', $data );
		$GLOBALS['smarty']->display('product_review.htm');  
	}
	
	//note 审核
	public function check_state(){
		$did = $_POST['did'];
		$now_state = $_POST['now_state'];
		$check_state = $_POST['check_state'];
		$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
		
		$pid_arr = $_POST['pid'];
		$pname_arr = $_POST['pname'];
		$star_arr = $_POST['star'];

		$did_str = implode(',', $did);
		
		$this->mod->check_state( $did_str, $check_state );
		
		//审核通过
		if( $check_state == 1 ){
			
			//向模板表Table_44中写入模板信息
			$str = '';
			foreach( $did as $v ){
				$this->_add_table( $pid_arr[$v], $pname_arr[$v] );
				
				//生成模板
				$str .= $this->_create( $pid_arr[$v] );
				
				//更新评论数
				$this->mod->update_review_num( $pid_arr[$v], $star_arr[$v] );
				
				//删除reveiw聚合页的缓存文件
				$this->_clearCache( $pid_arr[$v] );
			}
			
			
			$str .= '<script type="text/javascript">alert("选中项已通过！");document.location.href="product_review.php?action=index&web_type='.$this->web_type.'&check_state=0&page='.$page.'"</script>';
			echo $str;
			exit;


		//审核由通过状态变成未通过状态
		}elseif( $now_state == 1 && $check_state == 2 ){
			$str = '';
			foreach( $did as $v ){
				$this->mod->check_no_pass( $pid_arr[$v], $star_arr[$v] );
				
				//生成模板
				$str .= $this->_create( $pid_arr[$v] );
				
				//删除reveiw聚合页的缓存文件
				$this->_clearCache( $pid_arr[$v] );
			}
			
			$str .= '<script type="text/javascript">alert("选中项成功改成未通过！");document.location.href="product_review.php?action=index&web_type='.$this->web_type.'&check_state=0&page='.$page.'"</script>';
			echo $str;
			exit;
		}
		
		header("Location:product_review.php?action=index&web_type={$this->web_type}&check_state={$check_state}&page={$page}");
	}
	
	
	//note 删除
	public function del(){
		$did = $_POST['did'];
		$now_state = $_POST['now_state'];
		$check_state = $_POST['check_state'];
		
		$pid_arr = $_POST['pid'];
		$pname_arr = $_POST['pname'];
		$star_arr = $_POST['star'];
		
		$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
		
		$did_str = implode(',', $did);

		$str = '';
		foreach( $did as $v ){
			//删除评论
			$this->mod->del( $v );
			
			//从通过评论状态下删除
			if( $now_state == 1 ){
				
				$this->mod->check_no_pass( $pid_arr[$v], $star_arr[$v] );
			
				//生成模板
				$str .= $this->_create( $pid_arr[$v] );
			}
		}

		$str .= '<script type="text/javascript">alert("选中项删除成功！");document.location.href="product_review.php?action=index&web_type='.$this->web_type.'&check_state=0&page='.$page.'"</script>';
		echo $str;
		exit;
	}
	
	
	//note 推荐与取消推荐
	public function rec(){
		$did = $_POST['did'];
		$pid = $_POST['pid'];
		$is_rec = $_POST['is_rec'];
		
		$this->mod->rec( $did, $pid, $is_rec );
		
		$str = $this->_create( $pid );
		
		echo $str;
	}
	
	
	//note 取得待审核评论数
	public function wait_check(){
		echo $this->mod->get_wait_check( $this->web_type );
	}
	
	
	//note 设置产品是否有评论功能,有则向table中写入产品页面信息，无则删除
	public function set_review(){
		$pid = $_GET['pid'];
		$pname = $_GET['pname'];
		$is_review = $_GET['is_review'];
		
		$pname = str_replace( 'Wondershare' ,'', $pname );
		$pname = str_replace( 'iSkysoft' ,'', $pname );
		$pname = str_replace( 'Aimersoft' ,'', $pname );
		$pname = trim( $pname );
		
		if( $is_review ){
			$ret = $this->_add_table( $pid, $pname );
		}else{
			$ret = $this->mod->del_table( $pid, $this->web_type );
		}
		echo $ret;
	}
	
	//note 取得回复内容
	public function get_reply(){
		$did = $_GET['did'];
		$data = $this->mod->get_reply( $did );
		$data['reply'] =  str_replace('<br/>', "\r", $data['reply']);
		$data['reply'] =  str_replace('<br/>', "\n", $data['reply']);
		echo json_encode( $data );
	}
	
	
	//note 设置回复内容
	public function set_reply(){
		$did = $_POST['did'];
		$pid = $_POST['pid'];
		$reply = $_POST['reply'];
		
		$reply = str_replace("\r", '<br/>', $reply);
		$reply = str_replace("\n", '<br/>', $reply);
		$data = $this->mod->set_reply( $did, $reply );
		
		echo $this->_create( $pid );
	}
	
	
	//note 向客服转发用户评论信息
	public function transpood(){
		global $site_name;
		
		$did = $_POST['did'];
		$review_data = $this->mod->get_review_info( $did );
		$product_data = $this->mod->get_product_info( $review_data['pid'] );
		
		$arr = array();
		$arr['host'] = MAIL_HOST;
		$arr['from'] = MAIL_FROM;
		$arr['username'] = MAIL_USERNAME;
		$arr['password'] = MAIL_PASSWORD;
		$arr['subject'] = '【'. $site_name[$this->web_type] .'】站的产品评论处理';
		
		$content = file_get_contents('templates/product_review_mail_tpl.htm');
		$content = str_replace('-product_id-', $review_data['pid'], $content);
		$content = str_replace('-product_name-', $product_data['product_name'], $content);
		$content = str_replace('-star-', $review_data['star'], $content);
		$content = str_replace('-title-', str_replace('\\','',$review_data['title']), $content);
		$content = str_replace('-review-', str_replace('\\','',$review_data['review']), $content);
		$content = str_replace('-username-', $review_data['username'], $content);
		$content = str_replace('-email-', $review_data['email'], $content);
		$content = str_replace('-date-', $review_data['date'], $content);
		$arr['content'] = $content;

		$send = array('qiuln@wondershare.cn','weifang@wondershare.cn', 'liuhua@wondershare.cn', 'queyh@wondershare.cn');
		//$send = array('wangbo@wondershare.cn','huangcb@wondershare.cn');
		foreach( $send as $k=>$v ){
			if( $k > 0 ){
				$arr['subject'] = '抄送：'.$arr['subject'];
				sleep( 1 );
			}
			$arr['to'] = $v;
			$ret = $this->_sendMail( $arr );//var_dump( $ret );
		}
		
		$this->mod->transpood_ok( $did );
		
		echo 1;
	}
	
	//note 发送邮件
	private function _sendMail( $arr ){
		require_once '../plug/phpmailer/class.phpmailer.php';			
		$mail = new PHPMailer();
		$mail->IsSMTP();
		$mail->IsHTML(true);
		$mail->SMTPAuth = true;
		$mail->CharSet='UTF-8';
		$mail->Timeout = 60;
		$mail->Host = $arr['host'];
		$mail->From = $arr['from'];
		$mail->Username = $arr['username'];
		$mail->Password = $arr['password'];
		$mail->FromName =  $arr['from'];
		$mail->Subject = $arr['subject'];
		$mail->Body = $arr['content'];
		$mail->AddAddress($arr['to'], $arr['to']);
		$ret = $mail->Send();
		return $ret;
	}
	
	
	//note 向table_44中写入产品页面信息
	private function _add_table( $pid, $pname ){
		global $site_www;
		$pname = trim( $pname );
		$url = '/'.$site_www[$this->web_type].'/httpdocs/reviews/'.strtolower(str_replace(' ','-',$pname)).'/index.html';
		$this->mod->add_table( $url, $pid, $pname, $this->web_type );
	}
	
	//note 生成页面
	private function _create( $pid ){
		$ret = $this->mod->set_time( $pid, $this->web_type );
		$str = '';

		switch( $this->web_type ){
			case 1://ws
				//生成评论模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_44.php?action=pub&field_id=21624&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品单页模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_45.php?action=pub&field_id=20350&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_26.php?action=pub&field_id=20044&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
			break;
			
			case 2://aimersoft
				//生成评论模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_186.php?action=pub&field_id=21969&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_174.php?action=pub&field_id=21895&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品单页模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_175.php?action=pub&field_id=21901&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
			break;
			case 8://iskysoft
				//生成评论模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_193.php?action=pub&field_id=22043&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_137.php?action=pub&field_id=21669&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
				//生成产品单页模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_147.php?action=pub&field_id=21726&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
			break;
			case 4://applemacvideo
				//生成评论模板
				$str .= '<script src="http://cms.wondershare.com/bin/docList_250.php?action=pub&field_id=22315&key_id='.$pid.'&ajax_pub=1&page=1"></script>';
			break;
		}
		return $str;
	}
	
	
	//note 清除reveiw聚合页面的缓存文件
	function _clearCache( $pid ){
		if( in_array( $pid, array(2, 13, 114, 19, 105, 56))){
			$f_arr = glob( '../image2/cache/review__*.php' );
			foreach( $f_arr as $v ){
				if( file_exists( $v )){
					unlink( $v );
				}
			}
		}
	}
	
	
	//NOTE 有些星级数错误，调用此函数更新星级数
	function sys_star_num(){
		$pid = $_GET['pid'];
		if( !$pid ) exit('pid no allow empty');
		$ret = $this->mod->sys_star_num( $pid );
		echo $ret;
	}
	
}




$act = $_GET['action'];
$pr = new ProductReview( isset($_GET['web_type']) ? $_GET['web_type'] : 0, isset($_GET['menuid']) ? $_GET['menuid'] : 4 );

if( method_exists( $pr, $act ) ){
	$pr->$act();
}else{
	$pr->index();
}